import { request, type CustomAxiosRequestConfig } from '@/utils/request'
import type { ApiResponse } from '@/types/api'
import type {
  NotificationListResponse,
  UnreadCountResponse,
  NotificationFilterParams
} from '@/types/notification'

/**
 * 获取通知列表
 */
export const getNotifications = (params?: NotificationFilterParams): Promise<ApiResponse<NotificationListResponse>> => {
  return request.get<ApiResponse<NotificationListResponse>>('/notifications/', {
    params,
    showLoading: false,
    showError: true
  } as CustomAxiosRequestConfig).then(response => response.data)
}

/**
 * 获取未读通知数量
 */
export const getUnreadCount = (): Promise<ApiResponse<UnreadCountResponse>> => {
  return request.get<ApiResponse<UnreadCountResponse>>('/notifications/unread-count', {
    showLoading: false,
    showError: false // 不显示错误，避免频繁请求时弹出错误提示
  } as CustomAxiosRequestConfig).then(response => response.data)
}

/**
 * 标记通知为已读
 */
export const markNotificationAsRead = (notificationId: string): Promise<ApiResponse<null>> => {
  return request.patch<ApiResponse<null>>(`/notifications/${notificationId}/read`, {}, {
    showLoading: false,
    showError: true
  } as CustomAxiosRequestConfig).then(response => response.data)
}

/**
 * 标记所有通知为已读
 */
export const markAllNotificationsAsRead = (): Promise<ApiResponse<{ count: number }>> => {
  return request.patch<ApiResponse<{ count: number }>>('/notifications/read-all', {}, {
    showLoading: true,
    loadingText: '处理中...',
    showError: true,
    showSuccess: true
  } as CustomAxiosRequestConfig).then(response => response.data)
}

/**
 * 删除通知
 */
export const deleteNotification = (notificationId: string): Promise<ApiResponse<null>> => {
  return request.delete<ApiResponse<null>>(`/notifications/${notificationId}`, {
    showLoading: false,
    showError: true
  } as CustomAxiosRequestConfig).then(response => response.data)
}

/**
 * 批量删除通知
 */
export const deleteBatchNotifications = (notificationIds: string[]): Promise<ApiResponse<{ count: number }>> => {
  return request.delete<ApiResponse<{ count: number }>>('/notifications', {
    data: notificationIds,
    showLoading: true,
    loadingText: '删除中...',
    showError: true,
    showSuccess: true
  } as CustomAxiosRequestConfig).then(response => response.data)
}

